<?php


namespace app\community\controller;


use app\admin\controller\Together;
use app\admin\controller\Common;
use app\index\controller\Base;
use think\Cache;
use think\Db;
use think\Exception;
use think\Session;
use think\Validate;

class Answer extends Base
{

    //景区问答
    public function index()
    {
        $Together = new Together();
        $community = $Together->findColony();
        $this->assign("community", $community);
        $status = $keyword = $colony_id = "";
        $thisWechat = Session::get("thisWechat");
        $where['uniacid'] = $thisWechat['id'];
        if (!empty($_GET['colony_id'])) {
            $colony_id = $_GET['colony_id'];
            if ($colony_id == -1) {
                $where['colony_id'] = 0;
            } else {
                $where['colony_id'] = $colony_id;
            }
        }

        if (!empty($_GET['keyword'])) {
            $keyword = $_GET['keyword'];
            $where['title'] = ['like', "%" . $keyword . "%"];
        }

        if (!empty($_GET['status'])) {
            $status = $_GET['status'];
            $where['status'] = $status;
        }

        if (!empty($_GET['type'])) {
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;

                $list = Db::name("system_answer")
                    ->order("id desc")
                    ->where($where)->limit($start,$limit)->select();
                $count = Db::name("system_answer")
                    ->order("id desc")
                    ->where($where)->count("*");

                $status_val = [1 => "已发布", 3 => "已保存", 2 => "禁用"];
                foreach ($list as $k => $v) {
                    $list[$k]['add_name'] = "";
                    if (!empty($v['table'])) {
                        $list[$k]['add_name'] = $this->findUser($v['table'], $v['add_id']);
                    }
                    $list[$k]['update_name'] = "";
                    if (!empty($v['update_table'])) {
                        $list[$k]['update_name'] = $this->findUser($v['update_table'], $v['update_id']);
                    }
                    $list[$k]['status'] = '<span class="status status'.$v['status'].'">'.$status_val[$v['status']].'</span>';

                    $list[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
                    $list[$k]['updatetime'] = empty($v['updatetime'])?"未回复":date("Y-m-d H:i:s",$v['updatetime']);
                }
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }

        $assign = [
            "status" => $status,
            "keyword" => $keyword,
            "colony_id" => $colony_id
        ];
        $this->assign($assign);
        return $this->fetch('index');
    }

    public function findUser($table, $id)
    {
        $name = '';
        $user = Db::name($table)->where(['id' => $id])->find();
        if ($table == "account") {
            $name = $user['account'];
        } elseif ($table == "system_fans") {
            $name = $user['nickname'];
        } else {
            $name = $user['name'];
        }
        return $name;
    }


    //添加视图
    public function addAnswer()
    {
        $Together = new Together();
        $community = $Together->findColony();
        $this->assign("community", $community);

        if (!empty($_GET['id'])) {
            $id = $_GET['id'];
            $where['id'] = $id;
            $detail = Db::name("system_answer")->where($where)->find();
            $this->assign("detail", $detail);
            $newTag = explode(";", $detail['tag']);
            $tag = Db::name("system_article_tag")->where(['colony_id' => $detail['colony_id']])->select();

            foreach ($tag as $k => $v) {
                if (in_array($v['name'], $newTag)) {
                    $tag[$k]['check'] = 1;
                } else {
                    $tag[$k]['check'] = 0;
                }
            }
            $this->assign("tag", $tag);
            return $this->fetch('editAnswer');
        } else {
            return $this->fetch('addAnswer');
        }
    }

    //添加、修改操作
    public function doAnswer()
    {
        $common = new Common();
        Cache::clear();
        try {
            $rule = [
                ["colony_id", "require", "请选择所属景区"],
                ["title", "require", "请输入问题"],
                ["content", "require", "请输入回复内容"],
            ];
            $validate = new Validate($rule);
            $data = $this->request->post();
            if (!$validate->check($data)) {
                throw new Exception($validate->getError());
            }
            $table = Session::get("table");
            $thisWechat = Session::get("thisWechat");
            $data['uniacid'] = $thisWechat['id'];


            $data['update_table'] = $table;
            $data['update_id'] = Session::get("id");
            $data['updatetime'] = time();

            if (!empty($data['tag'])) {
                $data['tag'] = implode($data['tag'], ";");
            }
            if (empty($data['id'])) {
                $data['table'] = $table;
                $data['add_id'] = Session::get("id");
                $data['addtime'] = time();
                $id = Db::name("system_answer")->insertGetId($data);
            } else {
                $id = $data['id'];

                if($data['status']==1){
                    $answer = Db::name("system_answer")->where(['id'=>$id])->find();
                    $avatar = Db::name("config")->where(['id'=>$answer['uniacid']])->value('avatar');
                    $data2['remark'] = $data['content'];
                    $data2['name'] = Session::get("user_name");
                    $data2['portrait'] = $avatar;
                    $data2['addtime'] = time();
                    Db::name("system_answer_log")->where(['answer_id'=>$answer['id'],'type'=>2,'fans_id'=>$answer['add_id']])->update($data2);
                }





                Db::name("system_answer")->update($data);
            }

            $log['remark'] = "用户【" . Session::get("user_name") . "】操作景区问答信息,内容id【" . $id . "】";
            $log['controller'] = "Answer";
            $log['methods'] = "doAnswer";
            $common->addLog($log);

            $black['code'] = 0;
            $black['msg'] = "添加成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    //删除操作
    public function delAnswer()
    {
        Cache::clear();
        try {
            $id = $_POST['id'];
            $colony = Db::name("system_answer")->where(['id' => ['in', $id]])->select();
            $res = Db::name("system_answer")->where(['id' => ['in', $id]])->delete();

            $common = new Common();
            $log['controller'] = "Answer";
            $log['methods'] = "delAnswer";
            foreach ($colony as $k => $v) {
                $log['remark'] = "账号【" . Session::get("user_name") . "】删除信息【" . $v['title'] . "】id【" . $v['id'] . "】";
                $common->addLog($log);
            }
            $black['code'] = 0;
            $black['msg'] = "删除成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


}